package com.zhaopin.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhaopin.entity.ZhaoForm;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yin.peide
 * @date 2021-11-12 16:28
 */
@Service
public class ZhaopinService {

    // 声明了RestTemplate
    private final RestTemplate restTemplate;

    // 当bean 没有无参构造函数的时候，spring将自动拿到有参的构造函数，参数进行自动注入
    public ZhaopinService(RestTemplateBuilder restTemplateBuilder) {
        this.restTemplate = restTemplateBuilder.build();
    }


    public List<ZhaoForm> getZhaopin(Integer userId, String souFullIndex, Integer city, String exp) {
        List<ZhaoForm> list = new ArrayList<>();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString());


        JSONObject param = JSON.parseObject("{\n" +
                "    \"rows\": 60,\n" +
                "    \"start\": 0,\n" +
                "    \"debug\": {\n" +
                "        \"exp\": \"config-position_search-position_mbscore-default-c_APP_lambdaMart_labels_v1.1_project2\"\n" +
                "    },\n" +
                "    \"env\": \"prod\",\n" +
                "    \"userId\": \"633855797\",\n" +
                "    \"S_SOU_FULL_INDEX\": \"搜索产品经理\",\n" +
                "    \"S_SOU_WORK_CITY\": \"530\"\n" +
                "}");
        param.put("userId", userId.toString());
        param.put("S_SOU_FULL_INDEX", souFullIndex);
        param.put("S_SOU_WORK_CITY", city.toString());
        param.getJSONObject("debug").put("exp", exp);

        HttpEntity<JSONObject> formEntity = new HttpEntity<>(param, headers);
        ResponseEntity<String> resultResponseEntity = restTemplate.postForEntity("https://zpsearch.zhaopin.com/aggregator/position-search/position/mbscore", formEntity, String.class);
        String body = resultResponseEntity.getBody();
        JSONObject json = JSON.parseObject(body);
        JSONArray results = json.getJSONArray("results");
        int i = 1;
        for (Object result : results) {
            ZhaoForm zhaoForm = new ZhaoForm();
            JSONObject jsonObject = (JSONObject) result;
            zhaoForm.setI(i++);
            zhaoForm.setNumber(jsonObject.getString("number"));
            zhaoForm.setJobName(jsonObject.getString("jobName"));
            zhaoForm.setCityDisplay(jsonObject.getJSONObject("city").getString("display"));
            zhaoForm.setEduLevelName(jsonObject.getJSONObject("eduLevel").getString("name"));
            zhaoForm.setWorkingExpName(jsonObject.getJSONObject("workingExp").getString("name"));
            zhaoForm.setCompanyName(jsonObject.getJSONObject("company").getString("name"));
            zhaoForm.setCompanySizeName(jsonObject.getJSONObject("company").getJSONObject("size").getString("name"));
            zhaoForm.setCompanyTypeName(jsonObject.getJSONObject("company").getJSONObject("type").getString("name"));

//            if (!(jsonObject.getJSONObject("labels") == null)) {
//                zhaoForm.setLabels(jsonObject.getJSONObject("labels").toJSONString());
//                zhaoForm.setLabelsScore(jsonObject.getJSONObject("labels").getString("score"));
//            }else{
//                zhaoForm.setLabels("null");
//                zhaoForm.setLabelsScore("0");
//            }
//
//            if (!(jsonObject.getJSONObject("labels") == null)) {
//                if (jsonObject.getJSONObject("labels").getString("floors") != null) {
//                    zhaoForm.setFloors(jsonObject.getJSONObject("labels").getString("floors"));
//                }
//            }else{
//                zhaoForm.setFloors("null");
//            }

            if (!(jsonObject.getString("labels") == null)) {
                zhaoForm.setLabels(jsonObject.getString("labels"));
            }else{
                zhaoForm.setLabels("null");
            }

            //salary做判断
            if (jsonObject.getJSONObject("salary").getInteger("discuss") == 1) {
                zhaoForm.setSalary("面议");
            } else {
                Integer min = jsonObject.getJSONObject("salary").getInteger("min");
                Integer max = jsonObject.getJSONObject("salary").getInteger("max");
                zhaoForm.setSalary(min + "-" + max + "元/月");
            }
            zhaoForm.setSuperJobTypeFirstLevelName(jsonObject.getJSONObject("superJobType").getJSONObject("firstLevel").getString("name"));
            list.add(zhaoForm);
        }

        return list;

    }
}
